package com.doudizu.shoesonline.controller;

import com.doudizu.shoesonline.common.BaseResponse;
import com.doudizu.shoesonline.common.ResultUtils;
import com.doudizu.shoesonline.model.dto.chart.Performance;
import com.doudizu.shoesonline.model.dto.product.ProductQuery;
import com.doudizu.shoesonline.model.entity.Brand;
import com.doudizu.shoesonline.model.entity.Orders;
import com.doudizu.shoesonline.model.entity.Type;
import com.doudizu.shoesonline.model.vo.FormProductVO;
import com.doudizu.shoesonline.model.vo.ProductVO;
import com.doudizu.shoesonline.service.BrandService;
import com.doudizu.shoesonline.service.OrdersService;
import com.doudizu.shoesonline.service.ProductService;
import com.doudizu.shoesonline.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ln
 */
@RestController
@RequestMapping("/form")
@Api(tags = "报表模块")
public class FormController {

    @Resource
    private ProductService productService;
    @Autowired
    private BrandService brandService;

    @Resource
    private OrdersService ordersService;

    @Resource
    private TypeService typeService;

    @GetMapping("/product")
    @ApiOperation("产品报表")
    public BaseResponse productForm() {
        ProductQuery productQuery = new ProductQuery();
        List<ProductVO> list = (List<ProductVO>) productService.condition(productQuery);
        List<FormProductVO> voList = list.stream().map(productVO -> {
            FormProductVO formVO = new FormProductVO();
            formVO.setProductName(productVO.getProductName());
            formVO.setSales(productVO.getSales());
            return formVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(voList);
    }

    @GetMapping("/type")
    @ApiOperation("类型报表")
    public BaseResponse typeForm() {
        ProductQuery productQuery = new ProductQuery();
        List<ProductVO> list = (List<ProductVO>) productService.condition(productQuery);
        Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(ProductVO::getType, Collectors.summingInt(ProductVO::getSales)));

        List<Performance> mapList = new ArrayList<>();

        map.forEach((key,value) -> {
            Performance performance = new Performance();
            performance.setName(key);
            performance.setValue(value);
            mapList.add(performance);
        });
        List<Type> brands = typeService.list();
        List<String> legendData = new ArrayList<>(brands.size());
        legendData = brands.stream().map(item -> item.getTypeName()).collect(Collectors.toList());
        Map<String,List> res = new HashMap<>();
        res.put("legendData",legendData);
        res.put("seriesData",mapList);
        return ResultUtils.success(res);
    }

    @GetMapping("/performance")
    @ApiOperation("业绩报表")
    public BaseResponse brandForm() {
        ProductQuery productQuery = new ProductQuery();
        List<ProductVO> list = (List<ProductVO>) productService.condition(productQuery);
        Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(ProductVO::getBrand, Collectors.summingInt(ProductVO::getSales)));

        List<Performance> mapList = new ArrayList<>();

        map.forEach((key,value) -> {
            Performance performance = new Performance();
            performance.setName(key);
            performance.setValue(value);
            mapList.add(performance);
        });
        List<Brand> brands = brandService.list();
        List<String> legendData = new ArrayList<>(brands.size());
        legendData = brands.stream().map(item -> item.getBrandName()).collect(Collectors.toList());
        Map<String,List> res = new HashMap<>();
        res.put("legendData",legendData);
        res.put("seriesData",mapList);
        return ResultUtils.success(res);
    }

    @GetMapping("/flow")
    @ApiOperation("流量报表")
    public BaseResponse flowForm() {
        List<Orders> ordersList = ordersService.list();
        SimpleDateFormat format = new SimpleDateFormat("dd");

        Map<String, Long> map = ordersList.stream().filter(orders -> {
            Date payTime = orders.getPayTime();
            return payTime != null;
        }).map(orders -> {
            Date payTime = orders.getPayTime();
            return format.format(payTime);
        }).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        List<Performance> mapList = new ArrayList<>();

        map.forEach((key,value) -> {
            Performance performance = new Performance();
            performance.setName(key);
            performance.setValue(value.intValue());
            mapList.add(performance);
        });
        List<String> list = mapList.stream().map(Performance::getName).collect(Collectors.toList());
        List<Integer> list2 = mapList.stream().map(Performance::getValue).collect(Collectors.toList());
        Map<String,List> res = new HashMap<>();
        res.put("legendData",list);
        res.put("seriesData",list2);
        return ResultUtils.success(res);
    }
}
